package per.sen.shopping.domain.services.impl;


import cn.hutool.core.util.PhoneUtil;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.unchecked.Unchecked;
import io.vertx.mutiny.sqlclient.Row;
import io.vertx.mutiny.sqlclient.RowSet;
import io.vertx.mutiny.sqlclient.Tuple;
import jakarta.enterprise.context.ApplicationScoped;

import jakarta.inject.Inject;
import lombok.SneakyThrows;
import per.sen.shopping.domain.entities.SysBackendUserEntity;
import per.sen.shopping.domain.services.SysBackendUserDomain;
import per.sen.shopping.infrastructure.common.exception.BaseException;
import per.sen.shopping.infrastructure.common.exception.CustomException;
import per.sen.shopping.infrastructure.common.response.ApiResponse;
import per.sen.shopping.infrastructure.common.response.CustomCode;
import per.sen.shopping.infrastructure.configuration.DynamicReactiveSqlClientSelector;
import per.sen.shopping.infrastructure.repository.SysBackendUserReadRepository;
import per.sen.shopping.infrastructure.repository.SysBackendUserWriteRepository;
import per.sen.shopping.infrastructure.utils.RowToClazzUtil;
import per.sen.shopping.model.dto.SysBackendUserDTO;
import per.sen.shopping.model.po.SysBackendUserPO;

import java.util.*;

/**
 * @Describe: 领域服务实现类:用户
 * @Author: LYS
 * @Date: 2024/5/30 19:17
 */
@ApplicationScoped
public class SysBackendUserDomainImpl implements SysBackendUserDomain {

    @Inject
    SysBackendUserReadRepository sysBackendUserReadRepository;

    /**
     * @Describe: 用户登录
     * @Author: LYS
     * @Date: 2024/5/30 19:17
     */
    @Override
    public Uni<ApiResponse> login(SysBackendUserDTO sysBackendUserDTO) {
        String username = sysBackendUserDTO.getUsername();
        Uni<List<SysBackendUserPO>> sysBackendUserPOList ;
        if(PhoneUtil.isMobile(username)){
            sysBackendUserPOList =  sysBackendUserReadRepository.findByUserMobile(username);
        }else{
            sysBackendUserPOList = sysBackendUserReadRepository.findByUsername(username);
        }
        return sysBackendUserPOList.onItem().ifNull().failWith(() -> new CustomException(CustomCode.NO_USER.getCode(), CustomCode.NO_USER.getMessage()))
                .onItem().ifNotNull().transformToUni(Unchecked.function(list -> {
                    if (list.size() != 1) {
                        throw new CustomException(CustomCode.NO_USER.getCode(), CustomCode.NO_USER.getMessage());
                    }
                    SysBackendUserPO sysBackendUserPO = list.get(0);
                    if (SysBackendUserEntity.matchesPassword(sysBackendUserDTO.getPassword(), sysBackendUserPO.getPassword())) {
                        return Uni.createFrom().item(ApiResponse.success(sysBackendUserPO));
                    } else {
                        throw new CustomException(CustomCode.USERNAME_OR_PASSWORD_ERROR.getCode(), CustomCode.USERNAME_OR_PASSWORD_ERROR.getMessage());
                    }
                }));
    }

    @SneakyThrows
    @Override
    public String createToken(SysBackendUserPO sysBackendUserPO) {
        String username = sysBackendUserPO.getUsername();
        Map map = new HashMap();
        map.put("username",username);
        map.put("id",sysBackendUserPO.getUser_id());
        return Jwts.builder().
                setSubject(username).
                setIssuedAt(new Date()).
                setClaims(map).
                signWith(SignatureAlgorithm.HS256,"secrete12345611111111111111111111dfdsfdsfsdffsdfsdfsdf")
                .compact();




    }
}
